package com.jz.yanshe.ui.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jz.yanshe.R;
import com.jz.yanshe.base.BaseFragment;
import com.jz.yanshe.ui.AlipayActivity;
import com.jz.yanshe.ui.LoginActivity;
import com.jz.yanshe.ui.WXLoginActivity;
import com.jz.yanshe.util.PreferencesHelper;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/16.
 */

public class WebViewFragment extends BaseFragment {

    @BindView(R.id.webview)
    WebView webview;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_webview;
    }

    public static WebViewFragment newInstance(String type) {
        Bundle args = new Bundle();
        args.putString("TYPE", type);
        WebViewFragment fragment = new WebViewFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        WebSettings settings = webview.getSettings();
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        webview.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //该方法在Build.VERSION_CODES.LOLLIPOP以前有效，从Build.VERSION_CODES.LOLLIPOP起，建议使用shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead
                //返回false，意味着请求过程里，不管有多少次的跳转请求（即新的请求地址），均交给webView自己处理，这也是此方法的默认处理
                //返回true，说明你自己想根据url，做新的跳转，比如在判断url符合条件的情况下，我想让webView加载http://ask.csdn.net/questions/178242

                if (url.toString().contains("sina.cn")){
                    view.loadUrl("http://ask.csdn.net/questions/178242");
                    return true;
                }

                return false;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
            {
                //返回false，意味着请求过程里，不管有多少次的跳转请求（即新的请求地址），均交给webView自己处理，这也是此方法的默认处理
                //返回true，说明你自己想根据url，做新的跳转，比如在判断url符合条件的情况下，我想让webView加载http://ask.csdn.net/questions/178242
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    if (request.getUrl().toString().contains("sina.cn")){
                        view.loadUrl("http://ask.csdn.net/questions/178242");
                        return true;
                    }
                }

                return false;
            }

        });
    }

    @Override
    protected void initData() {
        String url =getArguments().getString("TYPE");
        String data = "<HTML><Div align=\"center\"  margin=\"0px\"><meta name=\"viewport\" charset=\"utf-8\" content=\"width=device-width,\n" +
                " initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/><style type=\"text/css\">img{width:100%;object-fit:cover;object-position:center;}</style>" + url + "</Div>";
//        webview.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null);
        webview.loadUrl(url);
        webview.addJavascriptInterface(new JsInteration(), "android");
    }

    public class JsInteration {

        @JavascriptInterface
        public void login() {
            startActivity(WXLoginActivity.class);
        }

        @JavascriptInterface
        public String gettoken() {
            if(TextUtils.isEmpty(PreferencesHelper.getInstance().getString(getActivity(),"token"))){
                return "1";
            }
            return PreferencesHelper.getInstance().getString(getActivity(),"token");
        }

        @JavascriptInterface
        public void pay(String orderid){
            System.out.println(orderid + " aaaaaaaaaaaaaaaaaa");
            Bundle bundle = new Bundle();
            bundle.putString("orderid",orderid);
            Intent mIntent = new Intent(getActivity(),AlipayActivity.class);
            mIntent.putExtras(bundle);
            getActivity().startActivity(mIntent);
        }

    }

}
